package com.qf.book.view;

import com.qf.book.domain.Book;
import com.qf.book.domain.User;
import com.qf.book.service.BookService;
import com.qf.book.service.UserService;
import com.qf.book.service.ipml.BookServiceImpl;
import com.qf.book.service.ipml.UserServiceImpl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Scanner;

public class bookSystem {
    public static void main(String[] args) {
        //1. 收集用户输入数据
        Scanner input = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = input.next();
        System.out.println("请输入密码:");
        String password = input.next();
        //2. 调用业务逻辑
        UserService userService = new UserServiceImpl();
        try {

            User user = userService.login(username,password);
            System.out.println("登录成功,欢迎 :" + user.getUsername());
            boolean flag = true;
            do {
                //3. 展示数据或操作结果
                BookService bookService = new BookServiceImpl();
                System.out.println("====1.查询所有图书==2.根据书名查询==3.新增图书==4.修改图书==5.删除图书==0.退出====");
                System.out.println("请选择");
                int choice = input.nextInt();
                switch (choice){
                    case 1:
                        List<Book> books = bookService.findAll();
                        printBooks(books);
                        break;
                    case 2:
                        System.out.println("请输入要查询的书名");
                        String title = input.next();
                        List<Book> bookList = bookService.findBytitle(title);
                        printBooks(bookList);
                        break;
                    case 3:
                        Book book = getBook();
                        try {
                            if (book != null) {
                                bookService.add(book);
                                System.out.println("新增图书成功");
                            }
                        } catch (Exception e) {
                            System.out.println("新增图书失败:"+ e.getMessage());
                        }
                        break;
                    case 4:
                        System.out.println("请输入要修改的图书编号");
                        int id = input.nextInt();
                        Book bk = bookService.findById(id);
                        if (bk != null){
                            //更新
                            try {
                                System.out.println("请输入新书名");
                                String newTitle=input.next();
                                System.out.println("请输入新价格");
                                double newPrice =input.nextDouble();
                                bk.setTitle(newTitle);
                                bk.setPrice(new BigDecimal(newPrice));
                                bookService.modfiy(bk);
                                System.out.println("更新成功");
                            } catch (Exception e) {
                                System.out.println("更新失败" + e.getMessage());
                            }
                        }else{
                            System.out.println("该图书不存在");
                        }
                        break;
                    case 5:
                        try {
                            System.out.println("请输入要删除的图书编号");
                            int bookId = input.nextInt();
                            bookService.remove(bookId);
                            System.out.println("删除图书成功");
                        } catch (Exception e) {
                            System.out.println("删除失败"+e.getMessage());
                        }
                        break;
                    case 0:
                        System.out.println("退出");
                        flag=false;
                        break;
                        default:
                            System.out.println("输入有误请重新输入");
                            break;
                }
            }while (flag);

        } catch (Exception e) {
            System.out.println("登录失败:" + e.getMessage());
        }
        System.out.println("谢谢使用");
    }
    //遍历图书
    public static void printBooks(List<Book> books){
        if (books!=null && books.size()>0){
            for (Book book : books) {
                System.out.println(book);
            }
        }
    }
    //新增图书
    public  static Book getBook(){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入书名");
        String title = input.next();
        System.out.println("请输入作者");
        String autor = input.next();
        System.out.println("请输入出版时间");
        String publicDate = input.next();
        System.out.println("请输入出版社名称");
        String publisher= input.next();
        System.out.println("请输入书名ISBN");
        String isbn = input.next();
        System.out.println("请输入图书价格");
        String price= input.next();
        System.out.println("请输入图书类别");
        int cid = input.nextInt();
        try {
            //封装图书
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Book book = new Book(0,title,autor,sdf.parse(publicDate),publisher,isbn,new BigDecimal(price),null,cid);
            return book;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
